#!/usr/bin/python
# @Time  : 2020/4/20 14:37
# @Author: LOUIE
# @Desc  : 变量上下文管理器
from utils.tools import format_variable


class Context(object):

    def __init__(self, variables=None):
        variables_pool = format_variable(variables or {})
        self.session_variables_pool = variables_pool
        self.test_variables_pool = {}
        self.init_variables()

    def init_variables(self, variables_pool=None):

        variables_pool = format_variable(variables_pool or {})
        variables_pool.update(self.session_variables_pool)
        parsed_variables_pool = variables_pool

        self.test_variables_pool = {}

        self.test_variables_pool.update(parsed_variables_pool)
        self.test_variables_pool.update(self.session_variables_pool)

    def update_test_variables(self, variables_pool):

        variables_pool = format_variable(variables_pool)
        self.test_variables_pool.update(variables_pool)

    def update_session_variables(self, variables_pool):

        variables_pool = format_variable(variables_pool)
        self.session_variables_pool.update(variables_pool)
        self.test_variables_pool.update(self.session_variables_pool)


if __name__ == '__main__':
    variables = {"address": "123456"}
    context = Context(variables)
    print(context.session_variables_pool)
    print(context.test_variables_pool)
    context.update_test_variables({'name': 'louie'})

    context.update_test_variables({'sex': 'nan'})
    print(context.session_variables_pool)
    print(context.test_variables_pool)

    variables2 = {"address2": "1234562"}
    context2 = Context(variables2)
    print(context2.session_variables_pool)
    print(context2.test_variables_pool)
    context2.update_test_variables({'name2': 'louie2'})

    context2.update_test_variables({'sex2': 'nan2'})
    print(context2.session_variables_pool)
    print(context2.test_variables_pool)
